package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.FeedbackItem;
import com.ruoyi.system.service.IFeedbackItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;

/**
 * 问题反馈Controller
 * 
 * @author ruoyi
 * @date 2025-01-12
 */
@RestController
@RequestMapping("/api/feedback")
public class FeedbackItemController extends BaseController
{
    @Autowired
    private IFeedbackItemService feedbackItemService;

    /**
     * 查询问题反馈列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(FeedbackItem feedbackItem)
    {
        startPage();
        List<FeedbackItem> list = feedbackItemService.selectFeedbackItemList(feedbackItem);
        TableDataInfo dataTable = getDataTable(list);

        return dataTable;
    }

    /**
     * 导出问题反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:export')")
    @Log(title = "问题反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeedbackItem feedbackItem)
    {
        List<FeedbackItem> list = feedbackItemService.selectFeedbackItemList(feedbackItem);
        ExcelUtil<FeedbackItem> util = new ExcelUtil<FeedbackItem>(FeedbackItem.class);
        util.exportExcel(response, list, "问题反馈数据");
    }

    /**
     * 获取问题反馈详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(feedbackItemService.selectFeedbackItemById(id));
    }

    /**
     * 新增问题反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:add')")
    @Log(title = "问题反馈", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult add(@RequestBody FeedbackItem feedbackItem)
    {
        int result = feedbackItemService.insertFeedbackItem(feedbackItem);
        if (result > 0) {
            FeedbackItem savedItem = feedbackItemService.selectFeedbackItemById(feedbackItem.getId());
            AjaxResult ajaxResult = AjaxResult.success("提交成功");
            ajaxResult.put("id", savedItem.getId());
            ajaxResult.put("serial", savedItem.getSerial());
            return ajaxResult;
        }
        return AjaxResult.error("提交失败");
    }

    /**
     * 修改问题反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:edit')")
    @Log(title = "问题反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeedbackItem feedbackItem)
    {
        return toAjax(feedbackItemService.updateFeedbackItem(feedbackItem));
    }

    /**
     * 处理反馈问题
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:edit')")
    @Log(title = "处理反馈问题", businessType = BusinessType.UPDATE)
    @PutMapping("/handle/{id}")
    public AjaxResult handleFeedback(@PathVariable("id") Long id, @RequestBody FeedbackItem feedbackItem)
    {
        feedbackItem.setId(id);
        // 补充处理人
        feedbackItem.setHandleBy(getUsername());
        return toAjax(feedbackItemService.updateFeedbackItem(feedbackItem));
    }

    /**
     * 删除问题反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:remove')")
    @Log(title = "问题反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(feedbackItemService.deleteFeedbackItemByIds(ids));
    }
}
